package form;

import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.TintucDAO;

import model.Muctintuc;
import model.Tintuc;

/**
 * Servlet implementation class Xulythemtintuc
 */
@WebServlet("/Xulythemtintuc")
public class Xulythemtintuc extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String command = "", hinh = "", title = "", title_khongdau = "",
			muctintuc = "", ngayviet = "", thoigianviet = "", sukien = "",
			noidung = "";
	private String err_image = "", err_title = "", err_titlekhongdau = "",
			err_muctintuc = "", err_ngayviet = "", err_thoigianviet = "",
			err_sukien = "", err_noidung;
	private Date dateWriter;
	private Time timeWriter;
	private int muctintuc_id;
	private boolean check=true;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Xulythemtintuc() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		toDo(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		toDo(request, response);
	}

	private void toDo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		command = request.getParameter("command");
		hinh = request.getParameter("hinh");
		title = request.getParameter("title");
		title_khongdau = request.getParameter("title_khongdau");
		
		muctintuc = request.getParameter("muctintuc");
		muctintuc_id = Integer.parseInt(muctintuc);
		
		sukien = request.getParameter("sukien");
		noidung = request.getParameter("noidung");

		try {
			ngayviet = request.getParameter("ngayviet");
			dateWriter = Date.valueOf(ngayviet);
		} catch (IllegalArgumentException e) {
			err_ngayviet = "Vui lòng nhập ngày viết!";
			request.setAttribute("err_ngayviet", err_ngayviet);
		}

		thoigianviet = request.getParameter("thoigianviet");
		err_thoigianviet="";
		if (thoigianviet == "") {
			err_thoigianviet = "Vui lòng nhập thời gian viết";
			request.setAttribute("err_thoigianviet", err_thoigianviet);
		} else {
			DateFormat dateFormat = new SimpleDateFormat("HH:mm");
			try {
				timeWriter = new Time(dateFormat.parse(thoigianviet).getTime());
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
	
		
		request.setAttribute("hinh", hinh);
		request.setAttribute("title", title);
		request.setAttribute("title_khongdau", title_khongdau);
		request.setAttribute("muctintuc", muctintuc);
		request.setAttribute("ngayviet", ngayviet);
		request.setAttribute("thoigianviet", thoigianviet);
		request.setAttribute("sukien", sukien);
		request.setAttribute("noidung", noidung);

		
	
		err_image = "";
		if (hinh == "") {
			err_image = "Vui lòng chọn hình ảnh!";
			request.setAttribute("err_image", err_image);
		}
		
		err_title = "";
		if (title == "") {
			err_title = "Vui lòng nhập tiêu đề tin tức!";
			request.setAttribute("err_title", err_title);
		}
		
		err_titlekhongdau = "";
		if (title_khongdau == "") {
			err_titlekhongdau = "Vui lòng tiêu đề không dấu!";
			request.setAttribute("err_titlekhongdau", err_titlekhongdau);
		}

		err_muctintuc = "";
		if (muctintuc == "") {
			err_muctintuc = "Vui lòng chọn mục tin tức!";
			request.setAttribute("err_muctintuc", err_muctintuc);
		}
		
		err_ngayviet = "";
		if (ngayviet == "") {
			err_ngayviet = "Vui lòng chọn ngày viết!";
			request.setAttribute("err_ngayviet", err_ngayviet);
		}
		
		
		err_sukien = "";
		if (sukien == "") {
			err_sukien = "Vui lòng chọn sự kiện! Nếu không có sự kiện thì chọn NO";
			request.setAttribute("err_sukien", err_sukien);
		}
		
		err_noidung ="";
		if(noidung==""){
			err_noidung ="Vui lòng nhập nội dung";
			request.setAttribute("err_noidung", err_noidung);
		}
		
		if (err_image.length() == 0 && err_title.length() == 0
				&& err_titlekhongdau.length() == 0
				&& err_muctintuc.length() == 0 && err_ngayviet.length() == 0
				&& err_thoigianviet.length() == 0 && err_noidung.length()==0) {
				check = false;
		}
		
		
		if (check==true) {
			RequestDispatcher rd = request.getServletContext()
					.getRequestDispatcher("/Admin_themtintuc.jsp");
			rd.forward(request, response);
		}
		
		
		if (command.equalsIgnoreCase("Add") && check == false ) {
			
			if (sukien.equalsIgnoreCase("NO")) {
				try {
					TintucDAO.insertNoEvent(new Tintuc(hinh, title,
							title_khongdau, new Muctintuc(muctintuc_id), dateWriter,
							timeWriter,noidung));
					RequestDispatcher rd = request.getServletContext()
							.getRequestDispatcher("/Admin_tintuc.jsp");
					rd.forward(request, response);
				} catch (ClassNotFoundException | SQLException e) {
					e.printStackTrace();
				}
			} else {
				try {
					TintucDAO.insertYesEvent(new Tintuc(hinh, title,
							title_khongdau, new Muctintuc(muctintuc_id), dateWriter,
							timeWriter, sukien, noidung));
					RequestDispatcher rd = request.getServletContext()
							.getRequestDispatcher("/Admin_tintuc.jsp");
					rd.forward(request, response);
				} catch (ClassNotFoundException | SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
